/**
 * Created by yangfakuan on 2018/10/19.
 */
(function (t) {
    if (typeof define === "function" && define.amd) {
        define(["jquery"], t)
    } else {
        t(jQuery)
    }
})(function (t) {
    var e = function (e, i) {
        var s = this;
        var a = {width: t(i).width(), height: t(i).height(), value: 0, min: 0, max: 20, onSelect: null, onChange: null};
        this.options = t.extend(a, e);
        this.cropData = {x: 0, y: 0, w: this.options.width, h: this.options.height, scaleX: 1, scaleY: 1};
        this.zoomData = {zoomValue: 0};
        this.init = function () {
            var e = '<div class="cascrop_holder unselect">' + '\t\t\t\t\t  <image class="cascrop_avatar" src="' + window.parent.localInfo.avatPath + '"></image>' + '\t\t\t          <image class="cascrop_target" style="display: none;"></image>' + "             \t  </div>" + '                 <div class="cascrop_slider_bar unselect"></div>';
            t(e).appendTo(t(i));
            t(i).addClass("unselect");
            var a = t(i).find(".cascrop_slider_bar"), n = t(i).find(".cascrop_holder");
            n.css({width: s.options.width + "px", height: s.options.height + "px"});
            a.SliderBar({
                value: s.options.value,
                min: s.options.min,
                max: s.options.max,
                disabled: true,
                change: function (t) {
                    s.zoomData.zoomValue = t;
                    s.zoom()
                }
            })
        };
        this.setImage = function (e) {
            var a = t(i).find(".cascrop_target"), n = t(i).find(".cascrop_holder"),
                o = {imageUrl: "", imageWidth: 0, imageHeight: 0};
            if (t(i).find(".cascrop_target").length > 0) {
                t(i).find(".cascrop_target").empty()
            }
            s.imgData = t.extend(o, e);
            s.cropData.w = s.options.width;
            s.cropData.h = s.options.height;
            n.css({width: s.options.width + "px", height: s.options.height + "px"});
            a.show();
            a.attr({"src": s.imgData.imageUrl});
            s.zoomData.zoomValue = s.options.value;
            a[0].onload = function () {
                s.zoom();
                s.updateCropData();
                a.draggable({
                    stop: function (t) {
                        s.adjustImage();
                        s.updateCropData()
                    }
                });
                t(i).find(".cascrop_slider_bar").SliderBar("reset");
                t(i).find(".cascrop_slider_bar").SliderBar("enable")
            }
        };
        this.adjustImage = function () {
            var e = parseInt(t(i).find(".cascrop_target").css("top")),
                a = parseInt(t(i).find(".cascrop_target").css("left")),
                n = parseInt(t(i).find(".cascrop_target").css("width")),
                o = parseInt(t(i).find(".cascrop_target").css("height")), r = s.options.width, l = s.options.height;
            if (n > r || o > l) {
                e = e > 0 ? 0 : e;
                e = e < -(o - l) ? -(o - l) : e;
                a = a > 0 ? 0 : a;
                a = a < -(n - r) ? -(n - r) : a
            } else {
                e = 0;
                a = 0
            }
            t(i).find(".cascrop_target").css({top: e + "px", left: a + "px"})
        };
        this.zoom = function () {
            var e = t(i).find(".cascrop_target"), a = 0, n = 0, o = 0, r = 0, l = s.imgData.imageWidth,
                d = s.imgData.imageHeight, c = s.options.width, p = s.options.height;
            if (s.zoomData.zoomValue < s.options.min) {
                o = s.options.width;
                r = s.options.height
            } else {
                var u = s.zoomData.zoomValue - s.options.min;
                var h = s.options.max - s.options.min;
                var f = parseInt(e.css("top"));
                var g = parseInt(e.css("left"));
                o = c + (l - c) * (u / h);
                r = p + (d - p) * (u / h);
                n = f * (r / d);
                a = g * (o / c)
            }
            o = o >= c ? o : c;
            r = r >= p ? r : p;
            if (l >= d) {
                o = l * r / d
            } else {
                r = d * o / l
            }
            setTimeout(function () {
                e.css({top: n, left: a, width: o, height: r});
                s.adjustImage();
                s.updateCropData()
            }, 100)
        };
        this.updateCropData = function () {
            var e = t(i).find(".cascrop_target");
            s.cropData.x = Math.abs(parseInt(e.css("left")));
            s.cropData.y = Math.abs(parseInt(e.css("top")));
            s.cropData.scaleX = parseInt(e.css("width")) / s.imgData.imageWidth;
            s.cropData.scaleY = parseInt(e.css("height")) / s.imgData.imageHeight;
            s.cropData.x = Math.round(s.cropData.x / s.cropData.scaleX);
            s.cropData.y = Math.round(s.cropData.y / s.cropData.scaleY);
            s.cropData.w = Math.round(s.options.width / s.cropData.scaleX);
            s.cropData.h = Math.round(s.options.height / s.cropData.scaleY);
            if (typeof s.options.onChange == "function") {
                s.options.onChange(s.cropData)
            }
        };
        this.init();
        return this
    };
    t.fn.Cascrop = function (t, i) {
        var s = this[0];
        if (typeof t == "object") {
            s.cascrop = new e(t, s)
        }
        if (typeof i == "function") {
            i.call(s.cascrop)
        }
        return this
    };
    var i = function (e, i) {
        var s = this;
        var a = {value: 0, min: 0, max: 0, disabled: false, change: null};
        this.options = t.extend(a, e);
        this.slider = {
            ui: {value: s.options.value},
            state: {isOnSlider: false, isDispatch: true, disabled: s.options.disabled},
            screenX: 0,
            sliderLength: 0,
            segmentLength: 0,
            dispatchTimer: null
        };
        this.init = function () {
            s.initUI();
            s.initEvent();
            s._prepareData();
            s._setValue(s.options.value, true)
        };
        this.initUI = function () {
            var e = '<div class="slider-bar-holder">' + '                 <div class="slider-bar-zoom-out unselect"></div>' + '\t\t\t\t  <div class="slider-bar-control unselect">' + '\t\t\t\t      <div class="slider-bar-slider">' + '\t\t\t\t\t      <div class="slider-bar-range"></div>' + '                         <div class="slider-bar-handler"></div>' + "\t\t\t\t      </div>" + "\t\t\t\t  </div>" + '\t\t\t      <div class="slider-bar-zoom-in unselect"></div>' + "\t\t\t  </div>";
            t(i).append(t(e))
        };
        this.initEvent = function () {
            t(i).on("click", ".slider-bar-zoom-out", function () {
                s._setValue(s.slider.ui.value - 1)
            }).on("click", ".slider-bar-zoom-in", function () {
                s._setValue(s.slider.ui.value + 1)
            }).on("click", ".slider-bar-control", function (t) {
                s._stopBubble(t);
                s._setPos(t.clientX - s.slider.screenX)
            }).on("mousedown", ".slider-bar-handler", function (t) {
                s._stopBubble(t);
                s.slider.state.isOnSlider = true
            }).on("mouseup", ".slider-bar-handler", function (t) {
                s._stopBubble(t);
                s.slider.state.isOnSlider = false
            });
            window.onmouseup = function (t) {
                s._stopBubble(t);
                s.slider.state.isOnSlider = false
            };
            window.onmousemove = function (t) {
                s._stopBubble(t);
                if (s.slider.state.isOnSlider && s._isOnMousedown(t) && t.offsetX >= 0 && t.clientX - s.slider.screenX >= 0 && t.clientX - s.slider.screenX <= s.slider.sliderLength) {
                    s._setPos(t.clientX - s.slider.screenX)
                }
            }
        };
        this._isOnMousedown = function (t) {
            var e = false;
            if (navigator.userAgent.indexOf("MSIE") > -1) {
                if (window.event.button == 1) {
                    e = true
                }
            } else if (navigator.userAgent.indexOf("Firefox") > -1) {
                if (t.buttons == 1) {
                    e = true
                } else {
                    e = false
                }
            } else if (navigator.userAgent.indexOf("Chrome") > -1) {
                if (t.buttons == 1) {
                    e = true
                }
            } else {
                e = true
            }
            if (!e) {
                s.slider.state.isOnSlider = false
            }
            return e
        };
        this._stopBubble = function (t) {
            if (t && t.stopPropagation) {
                t.stopPropagation()
            } else {
                window.event.cancelBubble = true
            }
        };
        this._prepareData = function () {
            s.slider.screenX = s._getX(t(i).find(".slider-bar-slider")[0]);
            s.slider.sliderLength = parseInt(t(i).find(".slider-bar-slider").width());
            s.slider.segmentLength = s.slider.sliderLength / (s.options.max - s.options.min)
        };
        this._setValue = function (t, e) {
            if (!e) {
                if (s.slider.state.disabled) {
                    return
                }
                if (t > s.options.max || t < s.options.min) {
                    return
                }
            }
            s._updateSlider(t * s.slider.segmentLength, e)
        };
        this._setPos = function (t) {
            if (s.slider.state.disabled) {
                return
            }
            s._updateSlider(t)
        };
        this._updateSlider = function (e, a) {
            if (e > s.slider.sliderLength || e < 0) {
                return
            }
            var n = s.slider.segmentLength;
            var o = Math.ceil(e / n);
            var r = e - (o - 1) * n;
            var l = o * n - e;
            if (r <= l) {
                e = (o - 1) * n;
                s.slider.ui.value = o - 1
            } else {
                e = o * n;
                s.slider.ui.value = o
            }
            t(i).find(".slider-bar-handler").css("left", e + "px");
            t(i).find(".slider-bar-range").css("width", e + "px");
            if (typeof s.options.change == "function" && s.slider.state.isDispatch && !a) {
                s.options.change(s.slider.ui.value);
                s.slider.state.isDispatch = false;
                s.slider.dispatchTimer = setTimeout(function () {
                    s.slider.state.isDispatch = true
                }, 20)
            }
        };
        this._getX = function (t) {
            return t.offsetLeft + (t.offsetParent ? this._getX(t.offsetParent) : t.x ? t.x : 0)
        };
        this.disable = function () {
            s.slider.state.disabled = true
        };
        this.enable = function () {
            s.slider.state.disabled = false
        };
        this.reset = function () {
            s._setValue(s.options.value, true)
        };
        this.init();
        return this
    };
    t.fn.SliderBar = function (t) {
        var e = this[0];
        if (typeof t == "object") {
            e.cascrop = new i(t, e)
        } else if (typeof t == "string") {
            switch (t) {
                case"disable": {
                    e.cascrop.disable();
                    break
                }
                case"enable": {
                    e.cascrop.enable();
                    break
                }
                case"reset": {
                    e.cascrop.reset()
                }
                default: {
                    break
                }
            }
        }
        return this
    }
});